/* eslint-disable */
let id = 1000;
import { getProjectMsg,looktProjectMsg ,lookNode,getType,getTemplate} from '@/api/planmanage'
export default {
  data() {
    return {
      currentPage: 1, //  el-pagination 初始页 
      // editproNodes:false,
      // 重置按钮
      companyFrom: {
        companyTel:"",
        assetName:"",
        assetType:"",
        assetLevel:"",
        statistics:"",
        startTime:"",
        endTime:"",
        construction:"",
        investment:"",
        legalUnit:"",
        site:""
      },

      dialogShow: {
        viewProject: false,
        editProject: false,
        insertproject:false,
        viewNode: false,
        editNode: false,
        viewNodeDetail: false,
        editNodeDetail: false,
        
      },
      //查看当前项目节点
      nodedata:[{
        id: '',
        label: '',
        level: '',
        startTime: '',
        endTime: '',
        actualStartTime: '',
        actualEndTime: '',
        plannedHours: '',
        precondition: '',
        responsibleDepartment: '',
        desc: ''
      }],
      looktaskdata:{
        id: '',
        label: '',
        level: '',
        startTime: '',
        endTime: '',
        actualStartTime: '',
        actualEndTime: '',
        plannedHours: '',
        precondition: '',
        responsibleDepartment: '',
        desc: ''
      },

      typedatda:[
        {
          projecttype:{}
        }
      ],

       // 部门列表
       departmentList: [
        '投资与人才促进部',
        '规土部',
        '财务部',
        '工程部',
      ],
        // 新增项目节点
        appendNode: {
          title: '新增节点',
          status: false,
        },
        // 父级节点
        fatherNode: {},
        // 节点表单
        appendNodeFrom: {
          label: '立项', // 节点名称
          startTime: '2020-01-20', // 计划开始时间
          endTime: '2020-03-30', // 计划结束时间
          actualStartTime: '2020-01-20', // 实际开始时间
          actualEndTime: '2020-03-30', // 实际结束时间
          plannedHours: '2个月', // 计划工时
          precondition: '暂无条件', // 前置条件
          responsibleDepartment: '投资与人才促进部', // 责任部门
          desc: '工程备注信息' // 备注
        },
        timeValue: ['2020-01-20', '2020-03-30'],
        timeValueActual: ['2020-01-20', '2020-03-30'],
        // 验证
        appendNodeRules: {
          label: [
            { required: true, message: '请输入节点名称', trigger: 'blur' },
          ],
        },
     
      value: [],
      // 模板数据
      companyData:[
        {
          contractNo: '',
          assetName: '',
          assetType: '',
          assetLevel: '',
          startTime: '',
          endTime: '',
          construction: '',
          statistics: ''
        }
      ],
      // //编辑节点弹窗
      // editproNodes:{
      //   title: '编辑模板',
      //   status: false,
      // },

      // '新增模板'弹窗
      dialogCompany:{
        title: '新增项目',
        status: false,
        status1: false,
      },
      

      
      // '新增项目'表单验证
      companyRules: {
        assetName: [
          { required: true, message: '项目名称不能为空', trigger: 'blur' },
        ],
        assetType: [
          { required: true, message: '项目类型不能为空', trigger: 'blur' },
        ],
        assetLevel: [
          { required: true, message: '项目层级不能为空', trigger: 'blur' },
        ],
        statistics: [
          { required: true, message: '是否列统不能为空', trigger: 'blur' },
        ],
        startTime: [
          { required: true, message: '计划开始时间不能为空', trigger: 'blur' },
        ],
        endTime: [
          { required: true, message: '计划结束时间不能为空', trigger: 'blur' },
        ],
        construction: [
          { required: true, message: '主要参建单位不能为空', trigger: 'blur' },
        ],
        investment: [
          { required: true, message: '总投资不能为空', trigger: 'blur' },
        ],
        legalUnit: [
          { required: true, message: '项目法人单位不能为空', trigger: 'blur' },
        ],
        site: [
          { required: true, message: '建设地点不能为空', trigger: 'blur' },
        ]
      },
      //'查看模板'弹窗
      looktemplate:{
        title: '查看项目详情',
        status: false,
      },
      
      //'查看基本信息'
      lookbase:
        {
        // contractNo: '0001201911025861',
        assetName: '',
        assetType: '',
        assetLevel: '',
        startTime: '',
        endTime: '',
        construction: '',
        statistics:'',
        investment:'',
        legalUnit:'',
        site:'',
        },
      //模板数据
      template:[{
        idno:"",
        name:"",
        node:{
          id: '',
          label: '',
          level: '',
          startTime: '',
          endTime: '',
          actualStartTime: '',
          actualEndTime: '',
          plannedHours: '',
          precondition: '',
          responsibleDepartment: '',
          desc: ''}
      }],
      
      //修改模板弹窗
      updatetemplate:{
        title: '修改模板',
        status: false,
      },
      // 查看弹窗
      showemplate:{
        title: '查看节点',
        status: false,
      },
      // ‘修改模板’表单数据
      updateFrom:{
        companyName: '',
        companyContact: '',
        companyTel: '',
        requisitionLimit: '',
        restrictedType: '',
        repairAssetType: '',
      },
      //下拉列表
      selectedtype:"",
      // 编辑节点弹窗
     
    }
  },
  mounted() {
    this.fetchProject()
    this.fetchBaseMsg()
    this.fetchTaskData()
    this.fetchType()
    this.fetchTemplate()
    
  },
  methods: {

   // 编辑项目节点弹窗
   editproNodes(type){
     if(type==""){
       this.$alert("请选择一个模板")
    }
    else{
      this.template.forEach(v=>{
        if(v.name==type){
          this.nodedata= v.node
        }
      })
    }
    this.editproNodes.status=true
  },
      
    //获取项目基本信息列表
    fetchProject(){
      this.listLoading = true
      getProjectMsg().then(response => {
          this.companyData = response.data
          this.listLoading = false
    })
  },
  //获取某个项目基本信息
  fetchBaseMsg(){
    this.listLoading = true
    looktProjectMsg().then(response => {
        this.lookbase = response.data
        this.listLoading = false
    })
  },
  //获取某项目的所有节点
  fetchTaskData(){
    this.listLoading = true
    lookNode().then(response => {
        this.looktaskdata = response.data
        this.listLoading = false
    })
  },
  //获取项目类型
  fetchType(){
    this.listLoading = true
    getType().then(response => {
        this.projecttype = response.data.projecttype
        this.listLoading = false
    })
  },
  //获取模板
  fetchTemplate(){
    this.listLoading = true
    getTemplate().then(response => {
        // this.$alert(response.data[0])
        this.template = response.data
        this.listLoading = false
    })
  },
  // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
    },

    // 重置按钮
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },


    // 新增模板弹窗
    dialogCompanyOpen(row){
      console.log(row.companyName)
      if(row.companyName == undefined){
        this.dialogCompany.title = '新增项目'
        this.resetCompany()
      }else{
        this.dialogCompany.title = '更新模板'
        this.companyFrom = row
      }
      this.dialogCompany.status = true
    },
    //修改模板弹窗
    updateTemplateOpen(row){
      console.log(row.companyName)
      if(row.companyName == undefined){
        this.dialogCompany.title = '编辑项目'
        this.resetCompany()
      }else{
        this.dialogCompany.title = '编辑项目'
        this.companyFrom = row
      }
      this.dialogCompany.status = true
    },
    
    // 新增模板
    addCompany(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$message({
            type: 'success',
            message: '提交成功!'
          })
          this.dialogCompany.status = false
        } else {
          
        }
      });
    },

    // 重置新增模板表单
    resetCompany() {

      this.$alert("xxx"+this.selectTemplateIndex)

      this.companyFrom = {
        companyName: '',
        companyContact: '',
        companyTel: '',
        requisitionLimit: '',
        restrictedType: '',
        repairAssetType: '',
      }
    },

    // 删除模板
    delCompany(row){
      this.$confirm('正在删除'+  row.companyName  +'公司, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    
    // 重置节点信息
    resetNode() {
      this.appendNodeFrom = {
        label: '', // 节点名称
        startTime: '', // 计划开始时间
        endTime: '', // 计划结束时间
        actualStartTime: '', // 实际开始时间
        actualEndTime: '', // 实际结束时间
        plannedHours: '', // 计划工时
        precondition: '', // 前置条件
        responsibleDepartment: '', // 责任部门
        desc: '' // 备注
      }
    },

   
    // 新增节点提交
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.appendNodeFrom.id = id++
          this.appendNodeFrom.children = []
          if (!this.fatherNode.children) {
            this.$set(this.fatherNode, 'children', []);
          }
          this.fatherNode.children.push(this.appendNodeFrom);
          this.appendNode.status = false
        } else {
          return false;
        }
      });
    },
  
    // 更新节点
    updateNode() {
      this.$message({
        message: '节点已更新',
        type: 'success'
      });
    },

    // 删除节点
    remove(node, data) {
      this.$confirm('此操作会删除该节点下所有子节点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
        const parent = node.parent;
        const children = parent.data.children || parent.data;
        const index = children.findIndex(d => d.id === data.id);
        children.splice(index, 1);
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },

  }
}
